package com.citytransport.prokopjuk.JSONparser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.citytransport.prokopjuk.dao.DAOTram;
import com.citytransport.prokopjuk.exception.InvalidFormatFile;


public class JSONParser {
    static Logger logger = Logger.getLogger(DAOTram.class.getName());
    private BufferedReader reader;
    private static int SIZE_BUFF = 1024;
    private String cell;

    public JSONParser(File sourse) {
	try {
	    reader = new BufferedReader(new FileReader(sourse));
	} catch (FileNotFoundException e) {
	    logger.error(e);
	    try {
		reader.close();
	    } catch (IOException e1) {
		logger.error(e1);
	    }
	}
    }

    public JSONParser(String fileURL) {
	this(new File(fileURL));
    }

    public JSONParser() {
	reader = null;
    }

    public String getCell() {
	return cell;
    }

    public boolean hasNext() {
	try {
	    reader.mark(SIZE_BUFF);
	    if (reader.readLine() == null) {
		reader.reset();
		return false;
	    }
	    reader.reset();
	} catch (IOException e) {
	    logger.error(e);
	}
	return true;
    }

    public String nextDraft() {
	String temp;
	StringBuilder builder = new StringBuilder();
	try {
	    if ((temp = reader.readLine()).equals("{")) {
		builder.append(temp);
		do {
		    temp = reader.readLine();
		    builder.append(temp);

		} while (!temp.equals("}"));
	    }
	} catch (IOException e) {
	    logger.error(e);
	}
	return builder.toString().trim();
    }

    public void next() {

	if (!this.hasNext())
	    return;
	Matcher m = Pattern.compile("[^\\s]*").matcher(this.nextDraft());
	StringBuilder builder = new StringBuilder();
	while (m.find()) {
	    builder.append(m.group());
	}
	cell = builder.toString();
    }

    public void setSourse(String sourse) {
	try {
	    reader = new BufferedReader(new FileReader(sourse));
	} catch (FileNotFoundException e) {
	    logger.error(e);;
	    try {
		reader.close();
	    } catch (IOException e1) {
		logger.error(e);
	    }
	}
    }

    public boolean parseBoolean(String attribute) throws InvalidFormatFile {
	// if(Cell == null)
	// throw cell is null
	String res = "";
	Matcher m = Pattern.compile("\"" + attribute + "\":.*?[,,}]").matcher(
		this.getCell());
	if (m.find()) {
	    m = Pattern.compile("true|false").matcher(m.group());
	    m.find();
	    res = m.group();
	}
	return Boolean.parseBoolean(res);
    }

    public String parseString(String attribute) throws InvalidFormatFile {
	Matcher m = Pattern.compile("\"" + attribute + "\":.*?[,,}]").matcher(
		this.getCell());
	m.find();
	String res = m.group();
	int mistakePosition = 2;
	res = res.substring(res.indexOf(":") + mistakePosition, res.length()
		- mistakePosition);
	return res;
    }

    public int parseInt(String attribute) throws InvalidFormatFile {

	Matcher m = Pattern.compile("\"" + attribute + "\":.*?[,,}]").matcher(
		this.getCell());
	if (m.find()) {
	    m = Pattern.compile("[0-9]+").matcher(m.group());
	    m.find();
	}

	return Integer.parseInt(m.group());
    }

}
